package com.hxminer.hxwebs.repository;

import com.hxminer.hxwebs.model.UserAddress;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author:Charles.zhang
 * @Description
 * @Date: Created in 下午6:23 2018/5/15
 * @Modified by:
 */

@Transactional
public interface AddressRespository extends JpaRepository<UserAddress, Integer> {


    //通过用户名来查询用户的地址信息
    public List<UserAddress> findByUserName(String userName);

    //通过用户名来查询用户的地址信息
    public List<UserAddress> findByAddress(String address);

    //通过 用户名 和地址 来查询用户信息
    public UserAddress findByUserNameAndAddress(String userName,String address);


    //通过用户名 和地址 来删除
    public void deleteByUserNameAndAddress(String userName,String address);

    //根据用户名来修改
    @Modifying
    @Query(value = "update user_address u set u.receive_name = :newReceiveName " +
            "where u.user_name = :oldName",nativeQuery = true)
    public void updateAddressByName(@Param("oldName") String oldName,@Param("newReceiveName") String newReceiveName);



    @Modifying
    @Query(value = "update user_address u set u.receive_name = :newReceiveName " +
            ", u.address = :newAddress " +
            ", u.tel_phone = :newTelPhone " +
            ", u.cell_phone = :newCellPhone " +
            "where u.user_name = :oldName and u.address = :oldAddress",nativeQuery = true)
    public void updateAddressByNameAndAddress(@Param("oldName") String oldName,
                                              @Param("oldAddress") String oldAddress,
                                              @Param("newReceiveName") String newReceiveName,
                                              @Param("newTelPhone") String newTelPhone,
                                              @Param("newCellPhone") String newCellPhone,
                                              @Param("newAddress") String newAddress);






}
